* ESTIMATION OF LEAST SQUARES REDUCED FORMS

* DEP VAR ARE:  EXPECTED INFLATION - AT 6M, 12M, 24M, 3-5YRS;
*		LOG LAB DEMAND - AT T+1, T+2, T+3, T+4
*		OWN PRICE ANNUAL CHANGE - EXPECTED AT 12M, OBSERVED AT T+1, T+2, T+4
*		QUALITATIVE ASSESSMENTS

* ESTIMATE SAME MODEL IN 4 WAYS: WITH AND WITHOUT TRIMMING DATA AND WITH AND WITHOUT SAMPLE WEIGHTS.
* POINT RESULTS OF ALL COMBINATIONS (WEIGHT/TRIM) ARE REPORTED IN APPENDIX
* FIGS 2, 3, 4 IN MAIN TEXT REPORT RESULTS OF TRIMMED/UNWEIGHTED ESTIMATIONS

* FIRST LOOP (OVER WEIGHETED/UNWEIGHTED) ESTIMATES TRIMMED WEIGHTED AND TRIMMED UNWEIGHTED RESULTS
* SECOND LOOP (OVER WEIGHETED/UNWEIGHTED) ESTIMATES UNTRIMMED WEIGHTED AND UNTRIMMED UNWEIGHTED RESULTS

* COEFFICIENTS AND SES ARE STORED IN MATRIXES (BB- EXP INFL; DP - OWN PRICE; LD - LAB DEMAND; OO - QUAL ASSESSMENTS)
* MATRIXES ARE THEN SAVED AS DTA AND USED TO GENERATE TABLES AND FIGURES

* TRIMMED WEIGHTED/UNWEIGHTED

quietly{
	
	
use WorkingData, clear



forvalues w=0/1{  /* ** Loops over weighted/unweighted est ** */
	
	cap drop mypes	
	if $_w==1{
		gen mypeso = peso /* weighted est */
	}
	if $_w==0{
		gen mypeso = 1    /* unweighted est */
	}


	qui sum dp
	local ini=r(min)
	local end=r(max)
	local rows = $_end - $_ini + 1
	mat def BB=J($_rows,9,.)

	local explvar=" treatment dpre i.dim i.area i.setton5 "

	* Expected inflation at all horizons
	
	forvalues j=1/$_rows{
		local q=209+$_j
		mat BB[$_j,1]=$_q

		cap qui reg it6all $_explvar  [aw=mypeso] if trims !=1 & dp==$_q, r
		if _rc==0{
			mat BB[$_j,2]=_b[treat]
			mat BB[$_j,3]=_se[treat]
		}

		cap qui reg it12all $_explvar  [aw=mypeso] if trims !=1 & dp==$_q, r
		if _rc==0{
			mat BB[$_j,4]=_b[treat]
			mat BB[$_j,5]=_se[treat]
		}

		cap qui reg it24all $_explvar  [aw=mypeso] if trims !=1 & dp==$_q, r
		if _rc==0{
			mat BB[$_j,6]=_b[treat]
			mat BB[$_j,7]=_se[treat]
		}

		cap qui reg it48all $_explvar  [aw=mypeso] if trims !=1 & dp==$_q, r
		if _rc==0{
			mat BB[$_j,8]=_b[treat]
			mat BB[$_j,9]=_se[treat]
		}

	}


	* Outlook qualitative variales

	mat def OO=J($ntrims,5,.)
	local r=1
	forvalues q=$firsttrim/$lasttrim{
		mat OO[$_r,1 ] = $_q

		local c=2
		local cc=3
		foreach y in PMIG SIMP {

			cap qui reg $_y $_explvar [aw=mypeso] if trims !=1 & dp==$_q, r
			if _rc==0{
				mat OO[$_r,$_c ] = _b[treat]
				mat OO[$_r,$_cc] = _se[treat]
			}

			local c  = $_c +2
			local cc = $_cc+2

		}

		local r = $_r + 1
	}


	* Own price annual changes: expected one year ahead, observed after one quarter, after 2 quarters,  after 4 quarters

	mat def DP=J($ntrims, 9, .)
	local r=1
	forvalues q=$firsttrim/$lasttrim{
		mat DP[$_r,1 ] = $_q

		local c=2
		local cc=3
		foreach y in dprez F1DPZ F2DPZ F4DPZ{

			cap qui reg $_y $_explvar [aw=mypeso] if trims !=1 & dp==$_q, r
			if _rc==0{
				mat DP[$_r,$_c ] = _b[treat]
				mat DP[$_r,$_cc] = _se[treat]
			}

			local c  = $_c +2
			local cc = $_cc+2

		}

		local r = $_r + 1
	}


	* Labor demand: log employment after one quarter, 2 qtrs, 3, qtrs, 4 qtrs

	mat def LD=J($ntrims, 9, .)
	local r=1
	forvalues q=$firsttrim/$lasttrim{
		mat LD[$_r,1 ] = $_q

		local c=2
		local cc=3
		foreach y in F1LAD F2LAD F3LAD F4LAD{

			cap qui reg $_y $_explvar [aw=mypeso] if trims !=1 & dp==$_q, r
			if _rc==0{
				mat LD[$_r,$_c ] = _b[treat]
				mat LD[$_r,$_cc] = _se[treat]
			}

			local c  = $_c +2
			local cc = $_cc+2

		}

		local r = $_r + 1
	}



	* Store coefficient estimates of exposure dummy for all outcom variables and associated 95%CI.
	
	* Expectations 
	preserve
	drop _all
	svmat BB

	gen l6=BB2-1.96*BB3
	gen u6=BB2+1.96*BB3

	gen l12=BB4-1.96*BB5
	gen u12=BB4+1.96*BB5

	gen l24=BB6-1.96*BB7
	gen u24=BB6+1.96*BB7

	gen l48=BB8-1.96*BB9
	gen u48=BB8+1.96*BB9

	rename BB2 dexp6
	rename BB4 dexp12
	rename BB6 dexp24
	rename BB8 dexp48

	rename BB1 dp
	keep dp dexp* u* l*

	keep if dp<=$lasttrim & dp>=$firsttrim
	format dp %tq
	replace dexp48=. if dp<=215
	replace l48 = . if dp<=215
	replace u48 = . if dp<=215

	save DataFigMeanGapTrim_Exp_W$_w, replace

	restore


	* Own prices 
	preserve
	drop _all
	svmat DP
	gen dp=DP1
	format dp %tq

	local i=2
	local ii=3
	foreach y in dprez F1DPZ F2DPZ F4DPZ{
		rename DP$_i bb_$_y
		rename DP$_ii se_$_y

		gen u_$_y=bb_$_y + 1.96*se_$_y
		gen l_$_y=bb_$_y - 1.96*se_$_y

		local i = $_i + 2
		local ii = $_ii + 2
	}

	reshape long bb_ se_ u_ l_, i(dp) j(var, string)


	save DataFigGapTrim_DPre_W$_w, replace

	restore

	* Labor demand

	preserve
	drop _all
	svmat LD
	gen dp=LD1
	format dp %tq

	local i=2
	local ii=3
	foreach y in F1LAD F2LAD F3LAD F4LAD {
		rename LD$_i bb_$_y
		rename LD$_ii se_$_y

		gen u_$_y=bb_$_y + 1.96*se_$_y
		gen l_$_y=bb_$_y - 1.96*se_$_y

		local i = $_i + 2
		local ii = $_ii + 2
	}

	reshape long bb_ se_ u_ l_, i(dp) j(var, string)

	save DataFigGapTrim_LabDem_W$_w, replace

	restore


	*Qualitative fwd looking assessment

	preserve
	drop _all
	svmat OO
	gen dp=OO1
	format dp %tq

	local i=2
	local ii=3
	foreach y in PMIG SIMP{
		rename OO$_i bb_$_y
		rename OO$_ii se_$_y

		gen u_$_y=bb_$_y + 1.96*se_$_y
		gen l_$_y=bb_$_y - 1.96*se_$_y

		local i = $_i + 2
		local ii = $_ii + 2
	}

	reshape long bb_ se_ u_ l_, i(dp) j(var, string)

	save DataFigGapTrim_QualAss_W$_w, replace

	restore

}


	
}

* UNTRIMMED WEIGHTED/UNWEIGHTED

quietly{
	
use WorkingData, clear


forvalues w=0/1{
	
cap drop mypes	
if $_w==1{
gen mypeso = peso /* weighted est */
}
if $_w==0{
gen mypeso = 1    /* unweighted est */
}


qui sum dp
local ini=r(min)
local end=r(max)
local rows = $_end - $_ini + 1
mat def BB=J($_rows,9,.)

local explvar=" treatment dpre i.dim i.area i.setton5 "


forvalues j=1/$_rows{
local q=209+$_j
mat BB[$_j,1]=$_q

cap qui reg it6all $_explvar  [aw=mypeso] if dp==$_q, r
if _rc==0{
mat BB[$_j,2]=_b[treat]
mat BB[$_j,3]=_se[treat]
}

cap qui reg it12all $_explvar  [aw=mypeso] if dp==$_q, r
if _rc==0{
mat BB[$_j,4]=_b[treat]
mat BB[$_j,5]=_se[treat]
}

cap qui reg it24all $_explvar  [aw=mypeso] if dp==$_q, r
if _rc==0{
mat BB[$_j,6]=_b[treat]
mat BB[$_j,7]=_se[treat]
}

cap qui reg it48all $_explvar  [aw=mypeso] if dp==$_q, r
if _rc==0{
mat BB[$_j,8]=_b[treat]
mat BB[$_j,9]=_se[treat]
}

}


* Outlook variales
mat def OO=J($ntrims,5,.)
local r=1
forvalues q=$firsttrim/$lasttrim{
mat OO[$_r,1 ] = $_q

local c=2
local cc=3
foreach y in PMIG SIMP {

cap qui reg $_y $_explvar [aw=mypeso] if dp==$_q, r
if _rc==0{
mat OO[$_r,$_c ] = _b[treat]
mat OO[$_r,$_cc] = _se[treat]
}

local c  = $_c +2
local cc = $_cc+2

}

local r = $_r + 1
}


* OWN PRICES, expected, obs next quarter, intwo quarter in 4 quarters

mat def DP=J($ntrims, 9, .)
local r=1
forvalues q=$firsttrim/$lasttrim{
mat DP[$_r,1 ] = $_q

local c=2
local cc=3
foreach y in dprez F1DPZ F2DPZ F4DPZ{

cap qui reg $_y $_explvar [aw=mypeso] if dp==$_q, r
if _rc==0{
mat DP[$_r,$_c ] = _b[treat]
mat DP[$_r,$_cc] = _se[treat]
}

local c  = $_c +2
local cc = $_cc+2

}

local r = $_r + 1
}


* labor demand


mat def LD=J($ntrims, 9, .)
local r=1
forvalues q=$firsttrim/$lasttrim{
mat LD[$_r,1 ] = $_q

local c=2
local cc=3
foreach y in F1LAD F2LAD F3LAD F4LAD{

cap qui reg $_y $_explvar [aw=mypeso] if dp==$_q, r
if _rc==0{
mat LD[$_r,$_c ] = _b[treat]
mat LD[$_r,$_cc] = _se[treat]
}

local c  = $_c +2
local cc = $_cc+2

}

local r = $_r + 1
}




* mean gap
preserve
drop _all
svmat BB

gen l6=BB2-1.96*BB3
gen u6=BB2+1.96*BB3

gen l12=BB4-1.96*BB5
gen u12=BB4+1.96*BB5

gen l24=BB6-1.96*BB7
gen u24=BB6+1.96*BB7

gen l48=BB8-1.96*BB9
gen u48=BB8+1.96*BB9

rename BB2 dexp6
rename BB4 dexp12
rename BB6 dexp24
rename BB8 dexp48

rename BB1 dp
keep dp dexp* u* l*

keep if dp<=$lasttrim & dp>=$firsttrim
format dp %tq
replace dexp48=. if dp<=215
replace l48 = . if dp<=215
replace u48 = . if dp<=215


save DataFigMeanGapUnTrim_Exp_W$_w, replace


restore


* own prices 
preserve
drop _all
svmat DP
gen dp=DP1
format dp %tq

local i=2
local ii=3
foreach y in dprez F1DPZ F2DPZ F4DPZ{
rename DP$_i bb_$_y
rename DP$_ii se_$_y

gen u_$_y=bb_$_y + 1.96*se_$_y
gen l_$_y=bb_$_y - 1.96*se_$_y

local i = $_i + 2
local ii = $_ii + 2
}

reshape long bb_ se_ u_ l_, i(dp) j(var, string)



save DataFigGapUnTrim_DPre_W$_w, replace


restore

* labor demand

preserve
drop _all
svmat LD
gen dp=LD1
format dp %tq

local i=2
local ii=3
foreach y in F1LAD F2LAD F3LAD F4LAD {
rename LD$_i bb_$_y
rename LD$_ii se_$_y

gen u_$_y=bb_$_y + 1.96*se_$_y
gen l_$_y=bb_$_y - 1.96*se_$_y

local i = $_i + 2
local ii = $_ii + 2
}

reshape long bb_ se_ u_ l_, i(dp) j(var, string)



save DataFigGapUnTrim_LabDem_W$_w, replace


restore


*qualitative fwd looking assessment

preserve
drop _all
svmat OO
gen dp=OO1
format dp %tq

local i=2
local ii=3
foreach y in PMIG SIMP{
rename OO$_i bb_$_y
rename OO$_ii se_$_y

gen u_$_y=bb_$_y + 1.96*se_$_y
gen l_$_y=bb_$_y - 1.96*se_$_y

local i = $_i + 2
local ii = $_ii + 2
}

reshape long bb_ se_ u_ l_, i(dp) j(var, string)



save DataFigGapUnTrim_QualAss_W$_w, replace


restore

}

}



***************** GENERATE LS FIGS *****************************


**** Fig 2 - EXPECTED INFLATION
quietly{
use DataFigMeanGapTrim_Exp_W0, replace

local q412=yq(2012,4)+0.5
local q411=yq(2011,4)+0.5
local q419=yq(2019,4)+0.5
local q422=yq(2022,4)+0.5

reshape long dexp u l, i(dp) j(hz)
format dexp l u %3.1f

lab def lhz 6 "A. 6 months" 12 "B. 12 months" 24 "C. 24 months" 48 "D. average 36-60 months"
lab val hz lhz

local q412=yq(2012,4)+0.5
local q411=yq(2011,4)+0.5
local q419=yq(2019,4)+0.5
local q422=yq(2022,4)+0.5

#delimit;
twoway rbar l u  dp , col(black) barw(0.1)  || scatter dexp dp,  msy(oh)  msi(*0.7) mco(red)   graphregion(c(white))
yla(-1(0.5)3, labsi(*0.7) angle(0)) ttick($_q411(4)$_q422)  ttick($_q411(4)$_q422 ) 
tli($_q411(4)$_q422, lc(ltbluishgray))  yli(0, lc(gs13)) yli(1, lc(gs15) lw(medthin))
tla(`=$_q411+2' "2012"  `=$_q412+2' "2013" `=$_q412+6' "2014" `=$_q412+10' "2015"  `=$_q412+14' "2016"  `=$_q412+18' "2017"  `=$_q412+22' "2018"  `=$_q412+26' "2019" `=$_q412+30' "2020" `=$_q412+34' "2021" `=$_q412+38' "2022", labsi(*0.7) tl(0))
by(hz, im(zero) row(2) note("") legend(off) graphregion(c(white))) xti("") yti("")  xsize(14) ysize(10) subti(,bc(white) si(*0.7));

graph export Fig2.eps, replace;
#delimit cr


* =================================
** THE FOLLOWIG FEW LINES PRINT OUT TO THE LOG FILE THE DATA (HORIZON, QUARTER, COEFFICIENTS, 95%CI) PLOTTED IN THE FIGURE
sort hz dp
format dp %tq
lab var l "Lower bound - 95%CI"
lab var u "Upper bound - 95%CI"
lab var dexp "LS Coeff - Exp Infl"
}
* *********************************************
* ********** DATA FOR FIG 2 *******************
* *********************************************
list hz dp dexp l u, linesize(255) header noobs
* =================================



**** Fig3 - QUALITATIVE ASSESS 
quietly{
use DataFigGapTrim_QualAss_W0, clear

gen Q=dp

local q412=yq(2012,4)+0.5
local q411=yq(2011,4)+0.5
local q418=yq(2018,4)+0.5
local q419=yq(2019,4)+0.5
local q422=yq(2022,4)+0.5

format u_ l_ bb_ %3.1f

  replace bb_=. if (var=="F1LAD"|var=="F1DPZ") & Q==yq(2022,4)
  replace u_=.  if (var=="F1LAD"|var=="F1DPZ") & Q==yq(2022,4)
  replace l_=.  if (var=="F1LAD"|var=="F1DPZ") & Q==yq(2022,4)
    
  replace bb_=. if (var=="F2LAD"|var=="F2DPZ") & Q>=yq(2022,3)
  replace u_=.  if (var=="F2LAD"|var=="F2DPZ") & Q>=yq(2022,3)
  replace l_=.  if (var=="F2LAD"|var=="F2DPZ") & Q>=yq(2019,3)

  replace bb_=. if (var=="F3LAD"|var=="F3DPZ") & Q>=yq(2022,2)
  replace u_=.  if (var=="F3LAD"|var=="F3DPZ") & Q>=yq(2022,2)
  replace l_=.  if (var=="F3LAD"|var=="F3DPZ") & Q>=yq(2022,2)
  
  replace bb_=. if (var=="F4LAD"|var=="F4DPZ") & Q>=yq(2022,1)
  replace u_=.  if (var=="F4LAD"|var=="F4DPZ") & Q>=yq(2022,1)
  replace l_=.  if (var=="F4LAD"|var=="F4DPZ") & Q>=yq(2022,1)  
  
  

lab def lv 1 "A. Probability that macro outlook improves in 3 months" 2 "B. Firm outlook in next 3 months"

gen v=1 if var =="PMIG"
replace v=2 if var=="SIMP"   

lab val v lv
  
#delimit;
twoway rbar l u  dp , col(black) barw(0.1)  || scatter bb dp,  msy(oh)  msi(*1) mco(red)   graphregion(c(white))
yla(, angle(0)) ttick($_q411(4)$_q422)  ttick($_q411(4)$_q422 ) yli(0, lc(gs13)) tli($_q411(4)$_q422, lc(ltbluishgray))  
tla(`=$_q411+2' "2012"  `=$_q412+2' "2013" `=$_q412+6' "2014" `=$_q412+10' "2015"  `=$_q412+14' "2016"  `=$_q412+18' "2017"  `=$_q412+22' "2018"  `=$_q412+26' "2019" `=$_q412+30' "2020" `=$_q412+34' "2021" `=$_q412+38' "2022", tl(0) )
by(v, im(zero) row(1) note("") legend(off) graphregion(c(white)) yrescale) xti("") yti("")  xsize(14) ysize(5) subti(,bc(white));

graph export Fig3.eps, replace;

#delimit cr

* =================================
** THE FOLLOWIG FEW LINES PRINT OUT TO THE LOG FILE THE DATA (HORIZON, QUARTER, COEFFICIENTS, 95%CI) PLOTTED IN THE FIGURE
sort v dp
format dp %tq
lab var l "Lower bound - 95%CI"
lab var u "Upper bound - 95%CI"
lab var bb "LS Coeff - Qual. Assess."
}
* *********************************************
* ********** DATA FOR FIG 3 *******************
* *********************************************
list v dp bb l u, linesize(255) header noobs
* =================================




*** Fig 4 - PRICES AND LAB DEM
quietly{
use DataFigGapTrim_LabDem_W0, clear
append using DataFigGapTrim_DPre_W0
gen Q=dp
local q412=yq(2012,4)+0.5
local q411=yq(2011,4)+0.5
local q418=yq(2018,4)+0.5
local q419=yq(2019,4)+0.5
local q422=yq(2022,4)+0.5
format u_ l_ bb_ %3.2f
  replace bb_=. if (var=="F1LAD"|var=="F1DPZ") & Q==yq(2022,4)
  replace u_=.  if (var=="F1LAD"|var=="F1DPZ") & Q==yq(2022,4)
  replace l_=.  if (var=="F1LAD"|var=="F1DPZ") & Q==yq(2022,4)
  replace bb_=. if (var=="F2LAD"|var=="F2DPZ") & Q>=yq(2022,3)
  replace u_=.  if (var=="F2LAD"|var=="F2DPZ") & Q>=yq(2022,3)
  replace l_=.  if (var=="F2LAD"|var=="F2DPZ") & Q>=yq(2022,3)
  replace bb_=. if (var=="F3LAD"|var=="F3DPZ") & Q>=yq(2022,2)
  replace u_=.  if (var=="F3LAD"|var=="F3DPZ") & Q>=yq(2022,2)
  replace l_=.  if (var=="F3LAD"|var=="F3DPZ") & Q>=yq(2022,2)
  replace bb_=. if (var=="F4LAD"|var=="F4DPZ") & Q>=yq(2022,1)
  replace u_=.  if (var=="F4LAD"|var=="F4DPZ") & Q>=yq(2022,1)
  replace l_=.  if (var=="F4LAD"|var=="F4DPZ") & Q>=yq(2022,1)
gen v=1 if var =="dprez"
replace v=2 if var=="F1DPZ"  
replace v=3 if var=="F2DPZ"
replace v=4 if var=="F4DPZ"
replace v=5 if var =="F1LAD"
replace v=6 if var=="F2LAD"  
replace v=7 if var=="F3LAD"
replace v=8 if var=="F4LAD"  


local q412=yq(2012,4)+0.5
local q411=yq(2011,4)+0.5
local q418=yq(2018,4)+0.5
local q419=yq(2019,4)+0.5
local q422=yq(2022,4)+0.5


gen low=-1.5 if v<=4
replace low=-0.15 if v>4
gen high=1.5 if v<=4
replace high=0.15 if v>4

lab def lv 1 "A. {&Delta}{it:p} - expected at assignment" 2 "B. {&Delta}{it:p} - 1 qtr after assignment" 3 "C. {&Delta}{it:p} - 2 qtrs after assignment" 4"D. {&Delta}{it:p} - 4 qtrs after assignment" 5 "E. {it:log L} - 1 qtr after assignment" 6 "F. {it:log L} - 2 qtrs after assignment" 7 "G. {it:log L} - 3 qtrs after assignment" 8 "H. {it:log L} - 4 qtrs after assignment"
lab val v lv


twoway rbar l_ u  dp , col(black) barw(0.1) || scatter bb dp,  msy(oh)  msi(*0.7) mco(red) ||scatter high low dp, msy(none none)  graphregion(c(white)) yla(, labsi(*0.7) angle(0)) ttick($_q411(4)$_q422)  ttick($_q411(4)$_q422 ) tli($_q411(4)$_q422, lc(ltbluishgray))   yli(0, lc(gs13)) tla(`=$_q411+2' "2012"  `=$_q412+2' "2013" `=$_q412+6' "2014" `=$_q412+10' "2015"  `=$_q412+14' "2016"  `=$_q412+18' "2017"  `=$_q412+22' "2018"  `=$_q412+26' "2019" `=$_q412+30' "2020" `=$_q412+34' "2021" `=$_q412+38' "2022", labsi(*0.5)  tl(0))  by(v, imargin(0) yrescale row(4) note("") legend(off) graphregion(c(white))) xti("") yti("")  xsize(14) ysize(16) subti(,bc(white) si(*0.7))

graph export Fig4.eps, replace

* =================================
** THE FOLLOWIG FEW LINES PRINT OUT TO THE LOG FILE THE DATA (HORIZON, QUARTER, COEFFICIENTS, 95%CI) PLOTTED IN THE FIGURE
sort v dp
format dp %tq
lab var l_ "Lower bound - 95%CI"
lab var u_ "Upper bound - 95%CI"
lab var bb "LS Coeff"
}
* *********************************************
* ********** DATA FOR FIG 4 *******************
* *********************************************
list v dp bb l_ u_, linesize(255) header noobs
* =================================

***************** GENERATE LS FIGS: END *****************************

***************** GENERATE LS TABS *****************************

* FOR EACH (GROUP OF) DEP VAR - 4 TABLES (ONE FOR EACH HZ) CONTAINING LS INFO EFFECTS FOR TRIMMED (t) AND UNTRIMMED (u) SAMPLES AND WEIGHTED (1) AND UNWEIGHTED (0) REGS

* ****EXPECTED INFLATION (TABS C1-C4)

foreach h in 6 12 24 48{

quietly{ 
	use DataFigMeanGapTrim_Exp_W0, clear
	keep dp dexp$_h u$_h l$_h
	gen se = (dexp - l)/1.96
	gen p = 2*(1-normal(abs(dexp/se)))
	gen sig=""
	replace sig="$^{*}$" if p<0.1
	replace sig="$^{**}$" if p<0.05 
	replace sig="$^{***}$" if p<0.01
	keep dp dexp se sig
	rename dexp$_h dexp_t0
	rename se se_t0
	rename sig sig_t0
	save TableRFExpInfl_$_h, replace

	use DataFigMeanGapTrim_Exp_W1, clear
	keep dp dexp$_h u$_h l$_h
	gen se = (dexp - l)/1.96
	gen p = 2*(1-normal(abs(dexp/se)))
	gen sig=""
	replace sig="$^{*}$" if p<0.1
	replace sig="$^{**}$" if p<0.05 
	replace sig="$^{***}$" if p<0.01
	keep dp dexp se sig
	rename dexp$_h dexp_t1
	rename se se_t1
	rename sig sig_t1
	merge 1:1 dp using TableRFExpInfl_$_h
	drop _m
	save TableRFExpInfl_$_h, replace


	use DataFigMeanGapUnTrim_Exp_W0, clear
	keep dp dexp$_h u$_h l$_h
	gen se = (dexp - l)/1.96
	gen p = 2*(1-normal(abs(dexp/se)))
	gen sig=""
	replace sig="$^{*}$" if p<0.1
	replace sig="$^{**}$" if p<0.05 
	replace sig="$^{***}$" if p<0.01
	keep dp dexp se sig
	rename dexp$_h dexp_u0
	rename se se_u0
	rename sig sig_u0
	merge 1:1 dp using TableRFExpInfl_$_h
	drop _m
	save TableRFExpInfl_$_h, replace

	use DataFigMeanGapUnTrim_Exp_W1, clear
	keep dp dexp$_h u$_h l$_h
	gen se = (dexp - l)/1.96
	gen p = 2*(1-normal(abs(dexp/se)))
	gen sig=""
	replace sig="$^{*}$" if p<0.1
	replace sig="$^{**}$" if p<0.05 
	replace sig="$^{***}$" if p<0.01
	keep dp dexp se sig
	rename dexp$_h dexp_u1
	rename se se_u1
	rename sig sig_u1
	merge 1:1 dp using TableRFExpInfl_$_h
	drop _m
	save TableRFExpInfl_$_h, replace

	format dexp* %3.2f
	format se* %6.3f
	format dp %tq

	gen z1="99"
	gen z2="99"
	gen z3="99"
	gen ll="\\"
	order dp dexp_t0 sig_t0 se_t0 z1 dexp_t1 sig_t1 se_t1 z2 dexp_u0 sig_u0 se_u0 z3 dexp_u1 sig_u1 se_u1 ll
	sort dp
	outsheet dp dexp_t0 sig_t0 se_t0 z1 dexp_t1 sig_t1 se_t1 z2 dexp_u0 sig_u0 se_u0 z3 dexp_u1 sig_u1 se_u1 ll using TabRFCoef_ExpInfl_$_h, replace delim("&")
}

* ==================================================================================================
* ***** THE FOLLOWING FEW LINES PRINT OLS RESULTS TO LOG FILE
* DEXP = LS COEFF; SIG = STAT SIG STARS; SE = STD ERR
* T = TRIMMED; U = UNTRIMMED
* 0 = UNWEIGHTED; 1 = WEIGHTED
* EXAMPLE - DEXP_T1 IS OLS COEFFS OF EXP INFL AT HORIZON $_H FROM TRIMMED UNWEIGHTED REG

* ***********************************
* ******** TABS C1-C4 ***************
* ***********************************
di " *******************************************************************"
di "OLS COEFF, SE AND STAT SIG OF EXP INFL AT HORIZON ", $_h, " MONTHS"
di " *******************************************************************"
list dp dexp_t0 sig_t0 se_t0 dexp_t1 sig_t1 se_t1 dexp_u0 sig_u0 se_u0 dexp_u1 sig_u1 se_u1 , linesize(255) header noobs
* ==================================================================================================
}

* ****OWN PRICES (TABS C5-C8)

foreach v in dprez F1DPZ F2DPZ F4DPZ{

quietly{
	
use DataFigGapTrim_DPre_W0, clear
keep if var=="$_v"
keep dp bb_ se_
gen p = 2*(1-normal(abs(bb_/se_)))
gen sig=""
replace sig="$^{*}$" if p<0.1
replace sig="$^{**}$" if p<0.05 
replace sig="$^{***}$" if p<0.01
keep dp bb se sig
rename bb_ bb_t0
rename se se_t0
rename sig sig_t0
save TableRF_$_v, replace

use DataFigGapTrim_DPre_W1, clear
keep if var=="$_v"
keep dp bb_ se_
gen p = 2*(1-normal(abs(bb_/se_)))
gen sig=""
replace sig="$^{*}$" if p<0.1
replace sig="$^{**}$" if p<0.05 
replace sig="$^{***}$" if p<0.01
keep dp bb se sig
rename bb_ bb_t1
rename se se_t1
rename sig sig_t1
merge 1:1 dp using TableRF_$_v
drop _m
save TableRF_$_v, replace


use DataFigGapUnTrim_DPre_W0, clear
keep if var=="$_v"
keep dp bb_ se_
gen p = 2*(1-normal(abs(bb_/se_)))
gen sig=""
replace sig="$^{*}$" if p<0.1
replace sig="$^{**}$" if p<0.05 
replace sig="$^{***}$" if p<0.01
keep dp bb se sig
rename bb_ bb_u0
rename se se_u0
rename sig sig_u0
merge 1:1 dp using TableRF_$_v
drop _m
save TableRF_$_v, replace

use DataFigGapUnTrim_DPre_W1, clear
keep if var=="$_v"
keep dp bb_ se_
gen p = 2*(1-normal(abs(bb_/se_)))
gen sig=""
replace sig="$^{*}$" if p<0.1
replace sig="$^{**}$" if p<0.05 
replace sig="$^{***}$" if p<0.01
keep dp bb se sig
rename bb_ bb_u1
rename se se_u1
rename sig sig_u1
merge 1:1 dp using TableRF_$_v
drop _m
save TableRF_$_v, replace

format bb* %3.2f
format se* %6.3f
format dp %tq

gen z1="99"
gen z2="99"
gen z3="99"
gen ll="\\"
order dp bb_t0 sig_t0 se_t0 z1 bb_t1 sig_t1 se_t1 z2 bb_u0 sig_u0 se_u0 z3 bb_u1 sig_u1 se_u1 ll
sort dp
outsheet dp bb_t0 sig_t0 se_t0 z1 bb_t1 sig_t1 se_t1 z2 bb_u0 sig_u0 se_u0 z3 bb_u1 sig_u1 se_u1 ll using TabRFCoef_$_v, replace delim("&")

}

* ==================================================================================================
* ***** THE FOLLOWING FEW LINES PRINT OLS RESULTS TO LOG FILE
* BB = LS COEFF; SIG = STAT SIG STARS; SE = STD ERR
* T = TRIMMED; U = UNTRIMMED
* 0 = UNWEIGHTED; 1 = WEIGHTED
* EXAMPLE - BB_T1 IS OLS COEFFS OF OWN PRICE $_V FROM TRIMMED UNWEIGHTED REG

* ***********************************
* ******** TABS C5-C8 ***************
* ***********************************
di "*******************************************************"
di "OLS COEFF, SE AND STAT SIG OF OWN PRICE ", "$_v" 
di "*******************************************************"
list dp bb_t0 sig_t0 se_t0 bb_t1 sig_t1 se_t1 bb_u0 sig_u0 se_u0 bb_u1 sig_u1 se_u1 , linesize(255) header noobs
* ==================================================================================================
}


* **** LABOR DEMAND (TABS C9-C12)

foreach v in F1LAD F2LAD F3LAD F4LAD{
	
quietly{

use DataFigGapTrim_LabDem_W0, clear
keep if var=="$_v"
keep dp bb_ se_
gen p = 2*(1-normal(abs(bb_/se_)))
gen sig=""
replace sig="$^{*}$" if p<0.1
replace sig="$^{**}$" if p<0.05 
replace sig="$^{***}$" if p<0.01
keep dp bb se sig
rename bb_ bb_t0
rename se se_t0
rename sig sig_t0
save TableRF_$_v, replace

use DataFigGapTrim_LabDem_W1, clear
keep if var=="$_v"
keep dp bb_ se_
gen p = 2*(1-normal(abs(bb_/se_)))
gen sig=""
replace sig="$^{*}$" if p<0.1
replace sig="$^{**}$" if p<0.05 
replace sig="$^{***}$" if p<0.01
keep dp bb se sig
rename bb_ bb_t1
rename se se_t1
rename sig sig_t1
merge 1:1 dp using TableRF_$_v
drop _m
save TableRF_$_v, replace


use DataFigGapUnTrim_LabDem_W0, clear
keep if var=="$_v"
keep dp bb_ se_
gen p = 2*(1-normal(abs(bb_/se_)))
gen sig=""
replace sig="$^{*}$" if p<0.1
replace sig="$^{**}$" if p<0.05 
replace sig="$^{***}$" if p<0.01
keep dp bb se sig
rename bb_ bb_u0
rename se se_u0
rename sig sig_u0
merge 1:1 dp using TableRF_$_v
drop _m
save TableRF_$_v, replace

use DataFigGapUnTrim_LabDem_W1, clear
keep if var=="$_v"
keep dp bb_ se_
gen p = 2*(1-normal(abs(bb_/se_)))
gen sig=""
replace sig="$^{*}$" if p<0.1
replace sig="$^{**}$" if p<0.05 
replace sig="$^{***}$" if p<0.01
keep dp bb se sig
rename bb_ bb_u1
rename se se_u1
rename sig sig_u1
merge 1:1 dp using TableRF_$_v
drop _m
save TableRF_$_v, replace

format bb* %3.2f
format se* %6.3f
format dp %tq

gen z1="99"
gen z2="99"
gen z3="99"
gen ll="\\"
order dp bb_t0 sig_t0 se_t0 z1 bb_t1 sig_t1 se_t1 z2 bb_u0 sig_u0 se_u0 z3 bb_u1 sig_u1 se_u1 ll
sort dp
outsheet dp bb_t0 sig_t0 se_t0 z1 bb_t1 sig_t1 se_t1 z2 bb_u0 sig_u0 se_u0 z3 bb_u1 sig_u1 se_u1 ll using TabRFCoef_$_v, replace delim("&")

}
	
* ==================================================================================================
* ***** THE FOLLOWING FEW LINES PRINT OLS RESULTS TO LOG FILE
* BB = LS COEFF; SIG = STAT SIG STARS; SE = STD ERR
* T = TRIMMED; U = UNTRIMMED
* 0 = UNWEIGHTED; 1 = WEIGHTED
* EXAMPLE - BB_T1 IS OLS COEFFS OF LAB DEMAND $_V FROM TRIMMED UNWEIGHTED REG

* ***********************************
* ******** TABS C9-C12 ***************
* ***********************************
di "*******************************************************"
di "OLS COEFF, SE AND STAT SIG OF LAB DEMAND ", "$_v" 
di "*******************************************************"
list dp bb_t0 sig_t0 se_t0 bb_t1 sig_t1 se_t1 bb_u0 sig_u0 se_u0 bb_u1 sig_u1 se_u1 , linesize(255) header noobs
* ==================================================================================================
}
	
***************** GENERATE LS TABS: ENDS *****************************
